package de.koller.aframe.util.web.basicHTML;

import java.awt.image.BufferedImage;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import javax.imageio.ImageIO;

abstract public class ImageSender implements IPage {

	protected final BasicWebServer wroot;

	public ImageSender( BasicWebServer wroot ) {
		this.wroot = wroot;
	}
	
	@Override
	public final void print( OutputStream out, String request ) throws IOException {
		ImageSender.print( out, getImage( request ) );
	}

	abstract public BufferedImage getImage( String request );
	
	public static void print( OutputStream out, BufferedImage img ) throws IOException {
		BufferedWriter o = new BufferedWriter( new OutputStreamWriter( out ) );
		o.write( "HTTP/1.0 200 OK\n" );
		o.write( "Content-Type: image/png\n\n" );
		
		ImageIO.write( img, "png", out );
	}

	@Override
	public boolean isRequestValid( String request ) {
		return true;
	}
	
}
